// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
#pragma once
#include "WeatherType.h"

namespace Weathr
{
    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class WeatherData sealed : Windows::UI::Xaml::Data::INotifyPropertyChanged
    {
    public:
        virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged;

        property bool IsToday
        {
        public:
            bool get() { return m_today; }
        internal:
            void set(bool value)
            {
                if (value != m_today)
                {
                    m_today = value; OnPropertyChanged("IsToday");
                }
            }
        }

        property Platform::String^ Description
        {
            Platform::String^ get() { return m_description; }
            void set(Platform::String^ value)
            {
                if (m_description != value)
                {
                    m_description = value; OnPropertyChanged("Description");
                }
            }
        }

        property int32 CurrentDisplayTemperature
        {
            int32 get() { return m_currentTemperature; }
            void set(int32 value)
            {
                if (m_currentTemperature != value)
                {
                    m_currentTemperature = value; OnPropertyChanged("CurrentDisplayTemperature");
                }
            }
        }

        property int32 ExpectedHiDisplayTemperature
        {
            int32 get() { return m_expectedHi; }
            void set(int32 value)
            {
                if (m_expectedHi != value)
                {
                    m_expectedHi = value; OnPropertyChanged("ExpectedHiDisplayTemperature");
                }
            }
        }

        property int32 ExpectedLoDisplayTemperature
        {
            int32 get() { return m_expectedLo; }
            void set(int32 value)
            {
                if (m_expectedLo != value)
                {
                    m_expectedLo = value; OnPropertyChanged("ExpectedLoDisplayTemperature");
                }
            }
        }

        property Platform::String^ DisplayWindSpeed
        {
            Platform::String^ get() { return m_displayWindSpeed; }
            void set(Platform::String^ value)
            {
                if (m_displayWindSpeed != value)
                {
                    m_displayWindSpeed = value; OnPropertyChanged("DisplayWindSpeed");
                }
            }
        }

        property Platform::String^ WindDirection
        {
            Platform::String^ get() { return m_windDirection; }
            void set(Platform::String^ value)
            {
                if (m_windDirection != value)
                {
                    m_windDirection = value; OnPropertyChanged("WindDirection");
                }
            }
        }

        property int32 WindDirectionDegree
        {
            int32 get() { return m_windDirectionDegree; }
            void set(int32 value)
            {
                if (m_windDirectionDegree != value)
                {
                    m_windDirectionDegree = value; OnPropertyChanged("WindDirectionDegree");
                }
            }
        }

        property float32 PrecipMM
        {
            float32 get() { return m_precipMM; }
            void set(float32 value)
            {
                if (m_precipMM != value)
                {
                    m_precipMM = value; OnPropertyChanged("PrecipMM"); OnPropertyChanged("IsPrecipitationExpected");
                }
            }
        }

        property Platform::String^ WeatherImagePathUri
        {
            Platform::String^ get() { return m_weatherImagePath; }
            void set(Platform::String^ value)
            {
                if (m_weatherImagePath != value)
                {
                    m_weatherImagePath = value; OnPropertyChanged("WeatherImagePath");
                }
            }
        }

        property bool IsPrecipitationExpected
        {
            bool get() { return m_precipMM > FLT_EPSILON; }
        }

    internal:
        WeatherData() : m_today(false), m_currentTemperature(0), m_expectedHi(0), m_expectedLo(0), m_windDirectionDegree(0), m_precipMM(0.0f)
        {
        }

        property int32 CurrentTemperatureF;
        property int32 ExpectedHiF;
        property int32 ExpectedLoF;

        property int32 CurrentTemperatureC
        {
            int32 get() { return ToCelsius(CurrentTemperatureF); }
        }
        property int32 ExpectedHiC
        {
            int32 get() { return ToCelsius(ExpectedHiF); }
        }
        property int32 ExpectedLoC
        {
            int32 get() { return ToCelsius(ExpectedLoF); }
        }

        property Windows::Foundation::DateTime Date;
        property std::wstring DebugDate; // Human-readable string to help with debugging.
        property WeatherType WeatherType;

        property int32 WindSpeedMph;
        property int32 WindSpeedKm
        {
            int32 get() { return static_cast<int32>(WindSpeedMph * 1.609344); }
        }

    private:
        bool m_today;
        Platform::String^ m_description;
        int32 m_currentTemperature;
        int32 m_expectedHi;
        int32 m_expectedLo;
        Platform::String^ m_displayWindSpeed;
        Platform::String^ m_windDirection;
        int32 m_windDirectionDegree;
        float32 m_precipMM;
        Platform::String^ m_weatherImagePath;

        inline void OnPropertyChanged(Platform::String^ propertyName)
        {
            PropertyChanged(this, ref new Windows::UI::Xaml::Data::PropertyChangedEventArgs(propertyName));
        }

        inline int32 ToCelsius(int32 fahrenheit)
        {
            return static_cast<int32>((fahrenheit - 32) * 0.5555555555555556);
        }
    };
}